home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2c.lha / p4-1.2c / lib / p4_secure.c < prev    next >
C/C++ Source or Header  |  1993-05-25  |  7KB  |  346 lines

  1. #include "p4.h"
  2. #include "p4_sys.h"
  3.  
  4. #if defined(SYMMETRY) || defined(SUN)  || \
  5.     defined(DEC5000)  || defined(SGI)  || \
  6.     defined(RS6000)   || defined(HP)   || \
  7.     defined(NEXT)     || defined(CRAY) || \
  8.     defined(CONVEX)   || defined(KSR)  || \
  9.     defined(FX2800)   || defined(FX2800_SWITCH)  || \
  10.     defined(SP1)
  11.  
  12. /**********************************
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <netdb.h>
  17. #include <pwd.h>
  18. #include <stdio.h>
  19. **********************************/
  20.  
  21. /* #define DEBUG */
  22.  
  23. char *start_prog_error;
  24.  
  25. extern int errno;
  26. extern char *sys_errlist[];
  27.  
  28. static int connect_to_server();
  29. static void send_string();
  30. static void recv_string();
  31.  
  32. int start_slave(host, username, prog, port, am_slave, pw_hook)
  33. char *host, *username, *prog, *am_slave;
  34. int port;
  35. char *(*pw_hook) ();
  36. {
  37.     int n, conn;
  38.     struct passwd *pw;
  39.     char port_string[250];
  40.     char pgm_args_string[250];
  41.     char *pw_string;
  42.     static char buf[250];
  43.     char *local_username;
  44.     char myhost[256];
  45.     int new_port, new_fd, stdout_fd;
  46.     char msg[500];
  47.     struct sockaddr_in temp;
  48.     int rc, templen;
  49.     int pid;
  50.  
  51.     myhost[0] = '\0';
  52.     get_qualified_hostname(myhost);
  53.     /* gethostname(myhost, sizeof(myhost)); */
  54.  
  55.     conn = connect_to_server(host);
  56.     if (conn < 0)
  57.     return -1;
  58.  
  59. #ifdef DEBUG
  60.     printf("Connected\n");
  61. #endif
  62.  
  63.     pw = getpwuid(geteuid());
  64.     if (pw == NULL)
  65.     {
  66.     extern char *getlogin();
  67.  
  68.     local_username = getlogin();
  69.     if (local_username == NULL)
  70.     {
  71.         start_prog_error = "Cannot get pw entry";
  72.         return -3;
  73.     }
  74.     }
  75.     else
  76.     {
  77.     local_username = pw->pw_name;
  78.     }
  79.  
  80.     send_string(conn, local_username);
  81.     send_string(conn, username);
  82.     recv_string(conn, buf, sizeof(buf));
  83. #ifdef DEBUG
  84.     printf("Got reply1 '%s'\n", buf);
  85. #endif
  86.  
  87.     if (strncmp(buf, "Password", 8) == 0)
  88.     {
  89.     if (pw_hook == NULL)
  90.         pw_string = "";
  91.     else
  92.         pw_string = (*pw_hook) (host, username);
  93.     send_string(conn, pw_string);
  94.     recv_string(conn, buf, sizeof(buf));
  95. #ifdef DEBUG
  96.     printf("Got reply '%s'\n", buf);
  97. #endif
  98.     if (strncmp(buf, "Proceed", 7) != 0)
  99.     {
  100.         start_prog_error = buf;
  101.         return -4;
  102.     }
  103.     }
  104.     else if (strncmp(buf, "Proceed", 7) != 0)
  105.     {
  106.     start_prog_error = buf;
  107.     return -4;
  108.     }
  109.  
  110.     send_string(conn, prog);
  111.  
  112.     sprintf(pgm_args_string, "%s %d %s", myhost, port, am_slave);
  113.     send_string(conn, pgm_args_string);
  114.  
  115.     if ((pid = fork_p4()) == 0)
  116.     {
  117.     net_setup_anon_listener(10, &new_port, &new_fd);
  118.     fflush(stdout);
  119.     sprintf(port_string, "%d", new_port);
  120.     send_string(conn, port_string);
  121.     fflush(stdout);
  122.     /* stdout_fd = net_accept(new_fd); */
  123.     templen = sizeof(temp);
  124.     SYSCALL_P4(stdout_fd, accept(new_fd, (struct sockaddr *) &temp, &templen));
  125.     close(new_fd);
  126.  
  127.     n = 1;
  128.     while (n > 0)
  129.     {
  130.         SYSCALL_P4(n, read(stdout_fd, msg, 499));
  131.         if (n > 0)
  132.         {
  133.         SYSCALL_P4(rc, write(1,msg,n));
  134.         fflush(stdout);
  135.         }
  136.     }
  137.     exit(0);
  138.     }
  139.  
  140.     recv_string(conn, buf, sizeof(buf));
  141. #ifdef DEBUG
  142.     printf("Got reply2 '%s'\n", buf);
  143. #endif
  144.     if (strncmp(buf, "Success", 7) != 0)
  145.     {
  146.     /* kill i/o handling process and decrement num forked */
  147.     kill(pid,SIGKILL);
  148.     p4_global->n_forked_pids--;
  149.     start_prog_error = buf;
  150.     return -4;
  151.     }
  152.  
  153.     start_prog_error = buf;
  154.     close(conn);
  155.  
  156. /***** Peter Krauss uses these lines
  157.     if (kill(pid, 0) == 0)
  158.         kill(pid, SIGKILL);
  159.     p4_dprintfl(00, "waiting for termination of anon_listener %d\n", pid);
  160. #if defined(DEC5000) || defined(HP) || defined(SUN)
  161.     waitpid(pid, (int *) 0, 0);
  162. #else
  163.     wait((int *) 0);
  164. #endif
  165. *****/
  166.  
  167.     return 0;
  168. }
  169.  
  170. static int connect_to_server(host)
  171. char *host;
  172. {
  173.     int conn;
  174.     int rc;
  175.     struct hostent *hostent;
  176.     struct sockaddr_in addr;
  177.  
  178.     SYSCALL_P4(conn, socket(AF_INET, SOCK_STREAM, 0));
  179.     if (conn < 0)
  180.     {
  181.     start_prog_error = sys_errlist[errno];
  182.     return -1;
  183.     }
  184.  
  185.     hostent = gethostbyname_p4(host);
  186.  
  187.     addr.sin_family = hostent->h_addrtype;
  188.     addr.sin_port = htons(sserver_port);
  189.     bcopy(hostent->h_addr, &addr.sin_addr, hostent->h_length);
  190.  
  191.     SYSCALL_P4(rc, connect(conn, (struct sockaddr *) & addr, sizeof(addr)));
  192.     if (rc < 0)
  193.     {
  194.     start_prog_error = sys_errlist[errno];
  195.     return -1;
  196.     }
  197.  
  198.     return conn;
  199. }
  200.  
  201. static void send_string(sock, str)
  202. int sock;
  203. char *str;
  204. {
  205.     int rc, len = strlen(str);
  206.     char nl = 10;
  207.  
  208.     SYSCALL_P4(rc, write(sock, str, len));
  209.     if (rc < 0)
  210.     {
  211.     perror("write");
  212.     p4_error("send_string write 1 ", -1);
  213.     }
  214.     SYSCALL_P4(rc, write(sock, &nl, 1));
  215.     if (rc < 0)
  216.     {
  217.     perror("write");
  218.     p4_error("send_string write 2 ", -1);
  219.     }
  220.  
  221. }
  222.  
  223. static void recv_string(sock, buf, len)
  224. int sock, len;
  225. char *buf;
  226. {
  227.     char *bptr;
  228.     int n;
  229.  
  230.     bptr = buf;
  231.     while (1)
  232.     {
  233.     SYSCALL_P4(n, read(sock, bptr, 1));
  234.     if (n < 0)
  235.     {
  236.         perror("read");
  237.         p4_error("recv_string read ", -1);
  238.         exit(1);
  239.     }
  240.     if (*bptr == '\n')
  241.         break;
  242.     bptr++;
  243.     if (bptr - buf >= len)
  244.         break;
  245.     }
  246.     *bptr = 0;
  247. }
  248.  
  249. #ifdef P4BSD
  250.  
  251. #include <sys/ioctl.h>
  252. static struct sgttyb orig_tty;
  253.  
  254. static int echo_off()
  255. {
  256.     struct sgttyb tty_new;
  257.  
  258.     if (ioctl(0, TIOCGETP, &orig_tty) < 0)
  259.     {
  260.     fprintf(stderr, "iotcl TIOCGETP failed: %s\n", sys_errlist[errno]);
  261.     return -1;
  262.     }
  263.  
  264.     tty_new = orig_tty;
  265.     tty_new.sg_flags &= ~(ECHO);
  266.  
  267.     if (ioctl(0, TIOCSETP, &tty_new) < 0)
  268.     {
  269.     fprintf(stderr, "iotcl TIOCSETP failed: %s\n", sys_errlist[errno]);
  270.     return -1;
  271.     }
  272. }
  273.  
  274. static int echo_on()
  275. {
  276.     if (ioctl(0, TIOCSETP, &orig_tty) < 0)
  277.     {
  278.     fprintf(stderr, "iotcl TIOCSETP failed: %s\n", sys_errlist[errno]);
  279.     return -1;
  280.     }
  281. }
  282.  
  283. #else
  284.  
  285. #include <termio.h>
  286.  
  287. struct termio tty_orig;
  288.  
  289. static int echo_off()
  290. {
  291.     struct termio tty_new;
  292.  
  293.     if (ioctl(0, TCGETA, &tty_orig) < 0)
  294.     {
  295.     fprintf(stderr, "tcgetattr failed: %s\n", sys_errlist[errno]);
  296.     return -1;
  297.     }
  298.  
  299.     tty_new = tty_orig;
  300.  
  301.     tty_new.c_lflag &= ~(ECHO);
  302.  
  303.     if (ioctl(0, TCSETA, &tty_new) < 0)
  304.     {
  305.     fprintf(stderr, "tcsetattr failed: %s\n", sys_errlist[errno]);
  306.     return -1;
  307.     }
  308. }
  309.  
  310. static int echo_on()
  311. {
  312.     if (ioctl(0, TCSETA, &tty_orig) < 0)
  313.     {
  314.     fprintf(stderr, "tcsetattr failed: %s\n", sys_errlist[errno]);
  315.     return -1;
  316.     }
  317. }
  318.  
  319. #endif
  320.  
  321. char *getpw(host, name)
  322. char *host, *name;
  323. {
  324.     static char buf[1024];
  325.     char *s;
  326.  
  327.     echo_off();
  328.     printf("Password for %s@%s: ", name, host);
  329.     fflush(stdout);
  330.     fgets(buf, sizeof(buf), stdin);
  331.     echo_on();
  332.     printf("\n");
  333.  
  334.     for (s = buf; *s; s++)
  335.     if (*s == '\n')
  336.     {
  337.         *s = 0;
  338.         break;
  339.     }
  340.  
  341.     return buf;
  342. }
  343.  
  344. #endif
  345. /* #ifdef SYMMETRY */
  346.